package com.whmskj.xjlhsc.mobie.menu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.domain.Yg01employeefiles;
import com.whmskj.xjlhbc.service.Yg01employeefilesService;
import cn.hutool.core.util.ObjectUtil;
import com.whmskj.xjlhsc.service.Managementofxt03Service;
import com.whmskj.xjlhsc.entity.Jobmenulinktable;
import com.whmskj.xjlhsc.entity.Scmenutablemoveend;
import com.whmskj.xjlhsc.jitapi.dto.Emp;
import com.whmskj.xjlhsc.mapper.JobmenulinktableMapper;
import com.whmskj.xjlhsc.mapper.ScmenutablemoveendMapper;
import com.whmskj.xjlhsc.mapper.Yg01employeefilesMapper;
import com.whmskj.xjlhsc.mobie.menu.service.MenuService;
import com.whmskj.xjlhsc.utils.SCUtil;
import org.apache.poi.hpsf.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 张
 * @version 1.0
 */

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private Yg01employeefilesMapper yg01employeefilesMapper;

    @Autowired
    private JobmenulinktableMapper jobmenulinktableMapper;

    @Autowired
    private ScmenutablemoveendMapper scmenutablemoveendMapper;
    
    @Autowired
    private Yg01employeefilesService yg01employeefilesService;
    
    @Autowired
    private Managementofxt03Service managementofxt03Service;
    @Override
    public List<Scmenutablemoveend> getRoleMenu() {
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        String typeOfWork = currentLoginEmp.getTypeOfWork();

        // 根据工种在managementOfXt03表中判断是否满足条件
        boolean canAccessScanPanel = managementofxt03Service.canAccessScanPanel(typeOfWork);

        List<Jobmenulinktable> jobmenulinktableList = jobmenulinktableMapper.selectRoleMenuId(typeOfWork);
        Jobmenulinktable jobmenulinktable = jobmenulinktableList.get(0);
        List<String> menuIdList = Arrays.stream(jobmenulinktable.getMenuid().split(",")).toList();

        List<Scmenutablemoveend> scmenutablemoveendList = scmenutablemoveendMapper.selectMenuList(menuIdList);
        
        // 根据managementOfXt03表判断结果过滤菜单
        if (canAccessScanPanel) {
            // 满足条件：只显示"扫码看台"相关菜单，隐藏"签到"菜单
            scmenutablemoveendList = filterMenusForProductionStaff(scmenutablemoveendList);
        } else {
            // 不满足条件：只显示"签到"相关菜单，隐藏"扫码看台"菜单
            scmenutablemoveendList = filterMenusForNonProductionStaff(scmenutablemoveendList);
        }
        
        List<Scmenutablemoveend> treeScmenutableMoveend = buildTree(scmenutablemoveendList);
        return treeScmenutableMoveend;
    }

    private List<Scmenutablemoveend> buildTree(List<Scmenutablemoveend> scmenutablemoveendList) {

        List<Scmenutablemoveend> treeList = new ArrayList<>();
        for (Scmenutablemoveend scmenutablemoveend : scmenutablemoveendList) {
            if (scmenutablemoveend.getParentmenuid()==null){
              treeList.add(findChild(scmenutablemoveend,scmenutablemoveendList));
            }
        }
        return treeList;
    }

    private Scmenutablemoveend findChild(Scmenutablemoveend scmenutablemoveend, List<Scmenutablemoveend> scmenutablemoveendList) {
        scmenutablemoveend.setChildMenu(new ArrayList<>());
        for (Scmenutablemoveend item : scmenutablemoveendList) {
            if (item.getParentmenuname()!=null && Integer.valueOf(item.getParentmenuid()).equals(scmenutablemoveend.getId())) {
                if (scmenutablemoveend.getChildMenu() == null) {
                    scmenutablemoveend.setChildMenu(new ArrayList<>());
                }
                scmenutablemoveend.getChildMenu().add(findChild(item,scmenutablemoveendList));
            }
        }
        return scmenutablemoveend;
    }
    
    
    /**
     * 判断是否为扫码看台相关菜单
     */
    private boolean isScanPanelMenu(Scmenutablemoveend menu) {
        String menuName = menu.getMenuname();
        String routePath = menu.getRoutepath();
        return (menuName != null && (menuName.contains("扫码看台") || menuName.contains("看台确认"))) ||
               (routePath != null && routePath.contains("/mob/scan"));
    }
    
    /**
     * 判断是否为签到相关菜单
     */
    private boolean isSignInMenu(Scmenutablemoveend menu) {
        String menuName = menu.getMenuname();
        String routePath = menu.getRoutepath();
        return (menuName != null && (menuName.contains("签到") || menuName.contains("扫码签到"))) ||
               (routePath != null && routePath.contains("/mob/scanScheduleShift"));
    }
    
    /**
     * 满足条件：只显示"扫码看台"相关菜单，隐藏"签到"菜单
     */
    private List<Scmenutablemoveend> filterMenusForProductionStaff(List<Scmenutablemoveend> menuList) {
        return menuList.stream()
            .filter(menu -> {
                // 如果是签到菜单，过滤掉
                if (isSignInMenu(menu)) {
                    return false;
                }
                // 否则保留（包括扫码看台菜单和其他无关菜单）
                return true;
            })
            .collect(java.util.stream.Collectors.toList());
    }
    
    /**
     * 不满足条件：只显示"签到"相关菜单，隐藏"扫码看台"菜单
     */
    private List<Scmenutablemoveend> filterMenusForNonProductionStaff(List<Scmenutablemoveend> menuList) {
        return menuList.stream()
            .filter(menu -> {
                // 如果是扫码看台菜单，过滤掉
                if (isScanPanelMenu(menu)) {
                    return false;
                }
                // 否则保留（包括签到菜单和其他无关菜单）
                return true;
            })
            .collect(java.util.stream.Collectors.toList());
    }
}
